//
//  PersonalViewController.m
//  YiqiMobile
//
//  Created by henry on 13-4-20.
//  Copyright (c) 2013年 henry. All rights reserved.
//

#import "PersonalViewController.h"
#import "Config.h"
#import "Utility.h"
#import "PersonalViewCell.h"
#import "AppDelegate.h"
#import "QuartzCore/QuartzCore.h"
#import "QuanziDetailViewController.h"
#import "EditPersonalViewController.h"
#import "BoweiListViewController.h"

@interface PersonalViewController ()

@end

@implementation PersonalViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.titleView=[Utility getTitleView:@"个人主页"];
    float tableHeight=UISCREEN_HEIGHT-22-44;
    self.mytableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, tableHeight) style:UITableViewStylePlain];
    self.mytableView.separatorStyle=UITableViewCellEditingStyleNone;
    self.mytableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    //self.mytableView.showsVerticalScrollIndicator=NO;
    self.mytableView.delegate=self;
    self.mytableView.dataSource=self;
    [self.view addSubview:self.mytableView];
    self.arrayList=[NSMutableArray array];
    [self.arrayList addObject:@[@{@"id":@1,@"name":@"我的参与一"},@{@"id":@1,@"name":@"我的参与一"},@{@"id":@1,@"name":@"我的参与一"}]];
    [self.arrayList addObject:@[@{@"id":@1,@"name":@"我的关注一"},@{@"id":@2,@"name":@"我的关注二"},@{@"id":@1,@"name":@"我的关注一"},@{@"id":@2,@"name":@"我的关注二"},@{@"id":@1,@"name":@"我的关注一"},@{@"id":@2,@"name":@"我的关注二"},@{@"id":@1,@"name":@"我的关注一"},@{@"id":@2,@"name":@"我的关注二"},@{@"id":@1,@"name":@"我的关注一"},@{@"id":@2,@"name":@"我的关注二"}]];
	// Do any additional setup after loading the view.
}
-(void) showData:(NSDictionary *)param{
    self.isOwer=true;
    if (self.isOwer) {
        self.topHeight=100;
    }else{
        self.topHeight=143;
    }
}

//表视图委托
#pragma mark - Tableview datasource & delegates
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:{
            return nil;
            break;
        }
        case 1:{
            UIImageView *view=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 29)];
            view.image=[Utility imageNamed:@"mp_bowen.png"];
            return view;
            break;
        }
        case 2:{
            UIImageView *view=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 29)];
            view.image=[Utility imageNamed:@"mp_caogao.png"];
            return view;
            break;
        }
        default:
            break;
    }
    return nil;
}

//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }
    return [[self.arrayList objectAtIndex:section-1] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section==0) {
        return 0.0f;
    }
    return 29.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return self.topHeight+90;
    }
    return 68.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentity = @"PersonalViewCell";
    static NSString *cellIdentity0 = @"PersonalViewCell0";
    PersonalViewCell *cell = nil;
    if (indexPath.section==0) {
        cell = (PersonalViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentity0];
        if (cell ==  nil) {
            cell = (PersonalViewCell*)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity0];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell addSubview:[self tableviewHeader]];
    }else {
        cell = (PersonalViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentity];
        if (cell ==  nil) {
            cell = [[PersonalViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity];
        }
        // NSDictionary *dict = [[self.arrayList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        cell.content.text=@"懒散的猫";
        cell.detail.text=[NSString stringWithFormat:@"来自圈子:%@",@"默克文化"];
        cell.tongji.text=[NSString stringWithFormat:@"供稿:%@",@"100"];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",@"圈子");
}


-(UIView *) tableviewHeader{
    UIView *header=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.topHeight+90)];
    UIImageView *background=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, self.topHeight)];
    if (self.isOwer) {
          background.image=[Utility imageNamed:@"mp_top_bg.png"];
    }else{
         background.image=[Utility imageNamed:@"mp_top_bg.png"];
    }
    [header addSubview:background];
    UIImageView *logo=[[UIImageView alloc] initWithFrame:CGRectMake(16, 9, 82, 82)];
    logo.layer.masksToBounds=YES;
    logo.layer.cornerRadius=41;
    logo.image=[Utility imageNamed:@"qz_logo.png"];
    [header addSubview:logo];
    
    NSString *nickName=@"Apple";
    UIFont *font=[UIFont fontWithName:FONT_FAMILY size:15.0f];
    CGSize size=[Utility getStringSize:nickName font:font withMaxSize:CGSizeMake(320, 20)];
    UILabel *nickname=[Utility detailTextLabel:CGRectMake(105, 19, size.width, 14)andFontSize:15.0f];
    nickname.textColor=UIColorFromRGB(0x000000);
    nickname.text=[NSString stringWithFormat:@"%@",nickName];
    [header addSubview:nickname];
    header.backgroundColor=[UIColor whiteColor];
    
    UILabel *detailLabel=[Utility detailTextLabel:CGRectMake(nickname.frame.origin.x, 50, 100, 14)andFontSize:12.0f];
    detailLabel.textColor=UIColorFromRGB(0x666666);
    detailLabel.text=[NSString stringWithFormat:@"简介:%@",@"一起躲猫猫把"];
    [header addSubview:detailLabel];
    header.backgroundColor=[UIColor whiteColor];
    if (self.isOwer) {
        [header addSubview:[Utility customButton:self action:@selector(editInfoAction:) normalImage:@"mp_edit_normal.png" lightedImage:@"mp_edit_hover.png" frame:CGRectMake(219, 16, 90, 31) title:nil tag:0]];
    }else{
        UILabel *leixing=[Utility detailTextLabel:CGRectMake(nickname.frame.origin.x+nickname.frame.size.width+10, 19, 100, 14)andFontSize:12.0f];
        leixing.textColor=UIColorFromRGB(0x666666);
        leixing.text=[NSString stringWithFormat:@"%@",@"一起成员"];
        [header addSubview:leixing];
        
        [header addSubview:[Utility customButton:self action:@selector(onclickButtonAction:) normalImage:@"mp_yqbutton_normal.png" lightedImage:@"mp_yqbutton_hover.png" frame:CGRectMake(18, 102, 89, 30) title:nil tag:HOMEPAGESECONTAG]];
        [header addSubview:[Utility customButton:self action:@selector(onclickButtonAction:) normalImage:@"mp_gxbutton_normal.png" lightedImage:@"mp_gxbutton_hover.png" frame:CGRectMake(119, 102, 89, 30) title:nil tag:HOMEPAGESECONTAG+1]];
        [header addSubview:[Utility customButton:self action:@selector(onclickButtonAction:) normalImage:@"mp_smsbutton_normal.png" lightedImage:@"mp_smsbutton_hover.png" frame:CGRectMake(220, 102, 89, 30) title:nil tag:HOMEPAGESECONTAG+2]];
    }
   
    
   
    
    
    
    //滚动视图
    self.personalMenu=[[PersonalMenu alloc] initWithFrame:CGRectMake(0, self.topHeight, self.view.frame.size.width, 90)];
    self.personalMenu.delegate=self;
    [header addSubview:self.personalMenu];
    NSArray *param=@[@{@"name":@"bowen",@"count":@"1000"}
                     ,@{@"name":@"gonggao",@"count":@"1000"}
                     ,@{@"name":@"pinglun",@"count":@"1000"}
                     ,@{@"name":@"pinglun",@"count":@"1000"}
                     ];
    [self.personalMenu showData:param];
    return header;
}

-(IBAction)onclickButtonAction:(id)sender{
    UIButton *button=(UIButton*)sender;
    if (button.tag==HOMEPAGESECONTAG) {//邀请
        NSLog(@"邀请");
        
    }else if(button.tag==HOMEPAGESECONTAG+1){//查看关系
        NSLog(@"查看关系");
    }else if(button.tag==HOMEPAGESECONTAG+2){//发私信
        NSLog(@"发私信");
    }
    
    
}

-(void) onclickScrollButton:(NSUInteger)category{
    switch (category) {
        case 0:{
            BoweiListViewController *boweiListViewController=[[BoweiListViewController alloc] initWithNibName:nil bundle:nil];
            [self.navigationController pushViewController:boweiListViewController animated:YES];
            break;
        }
        case 1:{
            
            break;
        }
        case 2:{
            
            break;
        }
        case 3:{
            
            break;
        }
        default:
            break;
    }
    NSLog(@"%d",category);
}

//管理员编辑个人资料
-(IBAction)editInfoAction:(UIButton*)sender{
    EditPersonalViewController *edit=[[EditPersonalViewController alloc] initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:edit animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
